#include "anim.h"

//数字雨
int columnIndex[8];
int stepIndex[8];
int glow[8];
bool Running[8]={false, false, false, false, false, false, false, false};
int stepsTillNext[8] = {0, 6, 2, 11, 4, 10, 7, 1};


/**************************************数字雨*********************************************** */
uint16_t hsv2rgb(Adafruit_NeoMatrix *matrix, uint16_t hue, uint8_t saturation, uint8_t value)
{
    uint8_t red = 0;
    uint8_t green = 0;
    uint8_t blue = 0;
    uint16_t hi = (hue / 60) % 6;
    uint16_t F = 100 * hue / 60 - 100 * hi;
    uint16_t P = value * (100 - saturation) / 100;
    uint16_t Q = value * (10000 - F * saturation) / 10000;
    uint16_t T = value * (10000 - saturation * (100 - F)) / 10000;

    switch (hi)
    {
    case 0:
        red = value;
        green = T;
        blue = P;
        break;
    case 1:
        red = Q;
        green = value;
        blue = P;
        break;
    case 2:
        red = P;
        green = value;
        blue = T;
        break;
    case 3:
        red = P;
        green = Q;
        blue = value;
        break;
    case 4:
        red = T;
        green = P;
        blue = value;
        break;
    case 5:
        red = value;
        green = P;
        blue = Q;
        break;
    default:
        return matrix->Color(255, 0, 0);
    }
    red = red * 255 / 100;
    green = green * 255 / 100;
    blue = blue * 255 / 100;
    return matrix->Color(red, green, blue);
}

//数字雨专用的移动像素特效
void movingPixel(Adafruit_NeoMatrix *matrix, int x, int y, int colorh)
{
    int pixel = y;
    matrix->drawPixel(x, pixel, hsv2rgb(matrix, colorh, 100, 100));
    if(pixel-1 >= 0){ matrix->drawPixel(x, pixel-1, hsv2rgb(matrix, colorh, 80, 80 ));}
    if(pixel-2 >= 0){ matrix->drawPixel(x, pixel-2, hsv2rgb(matrix, colorh, 70, 70 ));}
    if(pixel-3 >= 0){ matrix->drawPixel(x, pixel-3, hsv2rgb(matrix, colorh, 45, 45 ));}
    if(pixel-4 >= 0){ matrix->drawPixel(x, pixel-4, hsv2rgb(matrix, colorh, 25, 25 ));}
    if(pixel-5 >= 0){ matrix->drawPixel(x, pixel-5, hsv2rgb(matrix, colorh, 10, 10 ));}
    if(pixel-6 >= 0){ matrix->drawPixel(x, pixel-6, matrix->Color(0, 0, 0));}
}

//显示数字雨特效
void showRainingCode(Adafruit_NeoMatrix *matrix) 
{
    for(int i=0; i<8; i++)
    {
        if(stepIndex[i] > stepsTillNext[i])
        {
            Running[i] = true;
            stepsTillNext[i] = 13;  // 进行数组初始化
            columnIndex[i] = random((i*4), ((i+1)*4));
            glow[i] = random(0, 2);     //随机背景置灰值
            stepIndex[i] = 0;
        }
        if(Running[i] == true)
        {
            movingPixel(matrix, columnIndex[i], stepIndex[i], 240);
            if(stepIndex[i] == 13)
            {
                Running[i] = false;
            }
        }
        stepIndex[i] += 1;
    }
}
/**************************************数字雨*********************************************** */


/**************************************数字时钟*********************************************** */
//日期显示
void weekBarDisplay(Adafruit_NeoMatrix *matrix,int week) 
{
  week = week - 1;         
  for (int i = 0; i < 7; i++)
  {
    if ( i == 5 || i== 6 )
      matrix->drawFastHLine(3 + 4 * i, 7, 3, matrix->Color(50, 0, 0));
    else
      matrix->drawFastHLine(3 + 4 * i, 7, 3, matrix->Color(50, 50, 50));
  }
  if ( week == 5 || week == 6 )  //周六、日显示为红色
    matrix->drawFastHLine(3 + 4 * week, 7, 3, matrix->Color(255, 0, 0));
  else
    matrix->drawFastHLine(3 + 4 * week, 7, 3, matrix->Color(255, 255, 255));
}
/**************************************数字时钟*********************************************** */
